﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Formulaire extends CI_Controller {

	//formulaire inscription des participants
	public function inscriptionParticipant(){	
		$data['FK_LVL'] = $this->input->post('classe');
		$data['FK_SCHL'] = $this->model->getShoolFromGroup($this->input->post('groupe')); //get school from group
		$data['FK_GRP'] = $this->input->post('groupe');
		$data['USR_role'] = "membre";
		$data['USR_name'] = $this->input->post('nom');
		$data['USR_firstname'] = $this->input->post('prenom');
		$data['USR_email'] = $this->input->post('email');
		$data['USR_PWD'] = $this->input->post('pwd');
		$data['USR_account_valid'] = false;
		$data['USR_subject'] = NULL;
		
		$this->db->insert('tm_user_usr',$data);
		
		header('Location: '.base_url()); // redirection
	}
	
	//formulaire inscription jury
	public function inscriptionJury(){
		$data['FK_LVL'] = NULL;
		$data['FK_SCHL'] = $this->input->post('school'); //get school from group
		$data['FK_GRP'] = NULL;
		$data['USR_role'] = "jury";
		$data['USR_name'] = $this->input->post('nom');
		$data['USR_firstname'] = $this->input->post('prenom');
		$data['USR_email'] = $this->input->post('email');
		$data['USR_PWD'] = $this->input->post('pwd');
		$data['USR_account_valid'] = false;
		$data['USR_subject'] = $this->input->post('cats');
		
		$this->db->insert('tm_user_usr',$data);
		
		header('Location: '.base_url()); // redirection
	}
	
	public function valide(){
		$this->db->query("UPDATE tm_user_usr set USR_account_valid = 1 WHERE PK_USR = ".$this->input->post('id'));
		header('Location: '.base_url().'utilisateur/validation'); // redirection
	}
	
	//ajoute un groupe en ajax en BDD
	public function AjaxAddGroupe(){
		if(isset($_POST)){
			$nomGroupe = $this->input->post("nom");
			$school = $this->input->post("school");
			
			$data['GRP_LIB'] = $nomGroupe;
			$data['FK_SCHL'] = $school;
			
			$this->db->insert('tm_group_grp',$data);
			
			echo "<option value='".$this->db->insert_id()."' >".$nomGroupe."</option>";
		}else{
			header('Location: '.base_url()); //redirection
		}
	}
	
	//ajoute une ecole en ajax en BDD
	public function AjaxAddSchool(){
		if(isset($_POST)){
			$nom = $this->input->post("nom");
			$ville = $this->input->post("ville");
			
			$data['SCHL_CITY'] = $nom;
			$data['SCHL_LIB'] = $ville;
			
			$this->db->insert('tm_school_schl',$data);
			
			echo "<option value='".$this->db->insert_id()."' >".$nom."</option>";
		}else{
			header('Location: '.base_url()); //redirection
		}
	}
	
	public function connection(){
		$this->model->connectUser($this->input->post('login'), $this->input->post('pwd'));
		header('Location: '.base_url()); //redirection
	}
        public function CreateTacheFormulaire(){
            $data['LesItems'] = $this->model->ReadItem();
            $data['LesUsers'] = $this->model->ReadUsers();
            $this->template->load('template','creerTache',$data);
        }
        
        public function UpdateTacheFormulaire(){
            $data['UneTache'] = $this->model->ReadOneTache($this->uri->segment(3));
            $this->template->load('template','updateTache',$data);
        }
}